package _case

import "fmt"

// 基本接口，可用于变量的定义
type ToString interface {
	String() string
}

var s ToString //接口用于变量定义，但是泛型接口不支持

func (u user) String() string {
	return fmt.Sprintf("ID: %d,Name: %s,Age: %d", u.ID, u.Name, u.Age)
}
func (a address) String() string {
	return fmt.Sprintf("ID: %d,Province: %s,City: %d", a.ID, a.Province, a.City)
}

// 泛型接口，通过list转换成map
type GetKey[T comparable] interface {
	// 不写默认是any
	any
	Get() T
}

func (u user) Get() int64 {
	return u.ID
}
func (addr address) Get() int {
	return addr.ID
}

// 列表(切片)转集合
func listTomap[k comparable, T GetKey[k]](list []T) map[k]T {
	mp := make(MapT[k, T], len(list))
	for index_value, data := range list {
		mp[data.Get()] = data
		fmt.Println(index_value, data, data.Get())
	}
	return mp
}

func InterfaceCase() {
	userList := []GetKey[int64]{
		user{ID: 1, Name: "wangxd", Age: 11},
		user{ID: 2, Name: "wangxd2", Age: 22},
	}

	addList := []GetKey[int]{
		address{ID: 1, Province: "内蒙古", City: "呼和浩特"},
		address{ID: 2, Province: "内蒙古222", City: "呼和浩特222"},
	}
	userMp := listTomap[int64, GetKey[int64]](userList)
	fmt.Println(userMp)
	addMp := listTomap[int, GetKey[int]](addList)
	fmt.Println(addMp)
}
